package com.pioneer.gui.invoice.model;

import javax.swing.*;          //This is the final package name.
//import com.sun.java.swing.*; //Used by JDK 1.2 Beta 4 and all
import javax.swing.event.*;                               //Swing releases before Swing 1.1 Beta 3.							   
import javax.swing.plaf.metal.*;

import com.pioneer.gui.invoice.util.DropDownDataFactory;
//import com.pioneer.gui.invoice.model.DropDownValue;

import java.awt.*;
import java.awt.event.*;
import java.util.Vector;
import java.util.HashMap;

public class DropDownList
implements ComboBoxModel,VectorList
{
	//private InvoiceModel _model;	
	private Vector _dropDownValues=new Vector();
	private HashMap _nameKeyPairs=new HashMap();
	private String _dropDownValue=null;
	//private String _rollBackValue=null;
	
	public DropDownList()
	{
	}
	
	public DropDownList(String dbTable)
	{
		loadVector(dbTable);
	}
	
	
	public void loadVector(String dbTable)
	{		
		convertDBVectortoStringVector(dbTable,new DropDownDataFactory());
	}
	
	public void loadVector(String dbTable,String whereValue)
	{
		convertDBVectortoStringVector(dbTable,new DropDownDataFactory(whereValue));
	}
	
	private void convertDBVectortoStringVector(String dbTable, DropDownDataFactory dropDownDataFactory)
	{
		Vector nameKeyPairVector=dropDownDataFactory.getVector(dbTable);
		_dropDownValues=new Vector();
		_nameKeyPairs=new HashMap();
		for (int count=0;count<nameKeyPairVector.size();count++)
		{
			Object[] objArray=(Object[])nameKeyPairVector.get(count);
			_nameKeyPairs.put(objArray[1], objArray[0]);
			_dropDownValues.add(objArray[1]);
		}		
	}
	
	public Vector getDropDownValues()
	{
		return this._dropDownValues;
	}
	
	public Object getSelectedItem() 
	{
		//this._rollBackValue=this._dropDownValue;
		return this._dropDownValue;
	}
	
	public void addItem(Object obj)
	{
		this._dropDownValues.add(obj);
	}
	
	public void setSelectedItem(Object selected) 
	{
		this._dropDownValue=(String)selected;		
	}
	
	public void removeListDataListener(ListDataListener l)
	{
	}
	
	public void addListDataListener(ListDataListener l)
	{
	}
	
	public Object getElementAt(int index)
	{		
		return _dropDownValues.elementAt(index);		
	}
	
	public int getSize()
	{
		return _dropDownValues.size();
	}	
	
	public Object getDatabaseKey()
	{
		return _nameKeyPairs.get(_dropDownValue);
	}
}
	